package com.multiplayer.control;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Time;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.multiplayer.dao.IReservationDao;
import com.multiplayer.dao.ISeatDao;
import com.multiplayer.dao.IStudyRoomDao;
import com.multiplayer.dao.impl.ReservationDaoImpl;
import com.multiplayer.dao.impl.SeatDaoImpl;
import com.multiplayer.dao.impl.StudyRoomDaoImpl;
import com.multiplayer.entity.Seat;
import com.multiplayer.entity.StudyRoom;

/**
 * Servlet implementation class SeatServlet
 */
@WebServlet("/SeatServlet")
public class SeatServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    ISeatDao isd = new SeatDaoImpl(); 
    IStudyRoomDao isrd = new StudyRoomDaoImpl();
    IReservationDao ird = new ReservationDaoImpl();
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SeatServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String op = "";
		if(request.getParameter("op")!=null){
			op = request.getParameter("op");
		}
		String eseatno = "";
		if(request.getParameter("eseatno")!=null){
			eseatno = request.getParameter("eseatno");
		}
		String rno = "";
		if(request.getParameter("rno")!=null){
			rno = request.getParameter("rno");
		}
		String estatus = "";
		if(request.getParameter("estatus")!=null){
			estatus = request.getParameter("estatus");
		}
		
		if("all".equals(op)){
			allSeat(request, response, eseatno, rno, estatus);
		}else if("reserGetOne".equals(op)){
			reserGetOne(response, eseatno, rno, estatus);
		}else if("subRes".equals(op)){
			
			subRes(request, response, eseatno, rno);
		}else if("getByRno".equals(op)){
			StudyRoom studyRoom = isrd.queryStudyRoomById(rno).get(0);
			System.out.println(studyRoom.toString());
			Gson gson = new Gson();
			 String jsonString = gson.toJson(studyRoom);
			//响应
			 PrintWriter pw = response.getWriter();
			 pw.print(jsonString);
			 
			//关闭
			 pw.close();
		}
		
	
	
	}

	private void subRes(HttpServletRequest request, HttpServletResponse response, String eseatno, String rno)
			throws IOException {
		String estatus;
		//			eseatno:$("#eseatno").val(),
		//			rno:$("#rno").val(),
		//			restime:$("#restime").val(),
		//			resetime:$('resetime').val(),
				     SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");  
				     Date d = null;  
				     try {  
				         d = format.parse(request.getParameter("restime"));  
				     } catch (Exception e) {  
				         e.printStackTrace();  
				     }  
				     Time restime = new Time(d.getTime());  
				     try {  
				         d = format.parse(request.getParameter("resetime"));  
				     } catch (Exception e) {  
				         e.printStackTrace();  
				     }  
				     Time resetime = new Time(d.getTime());  
				     boolean flag = ird.addReservation(eseatno, restime, resetime);
				     if(flag == true){
				    	 estatus = "y";
				    	 Seat seat = new Seat(eseatno, rno, estatus);
				    	 flag = isd.editSeat(seat);
				    	 StudyRoom studyRoom = isrd.queryStudyRoomById(rno).get(0);
				    	 studyRoom.setRfreeseat(studyRoom.getRfreeseat()-1);
				    	 boolean flag2 = isrd.editStudyRoom(studyRoom);
				    	 flag = flag&&flag2;
				     }
				     
				     Gson gson = new Gson();
					 String jsonString = gson.toJson(flag);
					//响应
					 PrintWriter pw = response.getWriter();
					 pw.print(jsonString);
					 
					//关闭
					 pw.close();
	}

	private void reserGetOne(HttpServletResponse response, String eseatno, String rno, String estatus)
			throws IOException {
		List<Seat> list = isd.querySeatByThree(eseatno, rno, estatus);
		Seat seat = list.get(0);
		StudyRoom studyRoom = isrd.queryStudyRoomById(seat.getRno()).get(0);
		seat.setRno(studyRoom.getRno());
		Gson gson = new Gson();
		String jsonString = gson.toJson(seat);
		//响应
		PrintWriter pw = response.getWriter();
		pw.print(jsonString);
		
		//关闭
		pw.close();
	}

	private void allSeat(HttpServletRequest request, HttpServletResponse response, String eseatno, String rno, String estatus)
			throws ServletException, IOException {
		List<Seat> list = isd.querySeatByThree(eseatno, rno, estatus);
		System.out.println("list.size"+list.size());
		request.setAttribute("list", list);
		request.setAttribute("rno", rno);
		request.getRequestDispatcher("Seat.jsp").forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
